package com.ruoyi.web.rmNew.service.http;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.web.core.config.ConfigLoader;
import com.ruoyi.web.core.config.Rm2Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Slf4j
@Service
public class RmHttpService2 {
    @Autowired
    private ConfigLoader configLoader;
    @Autowired
    private Rm2Config config;

    public JSONObject getRmHeaders() {
        List<String> cookie = FileUtil.readLines(config.getRmPath() + "gamenames" + File.separator + "rm-cookie.txt", "utf8");
        JSONObject header = new JSONObject();
        header.put("cookie", cookie);
        return header;
    }

    public String sendRmGameList() {
        return HttpUtils.httpStr(ConfigLoader.getConfig().getRmHost() + "/market.php", "get", null, getRmHeaders());
    }

    public String sendRmGameDetail(String gameid, String path) {
        long beginTime = System.currentTimeMillis();
        String str = HttpUtils.httpStr(ConfigLoader.getConfig().getRmHost() + "/marketorder.php?gameId=" + gameid, "get", null, getRmHeaders());
        FileUtil.writeString(str, path, "utf8");
        log.info("耗时:{}", (System.currentTimeMillis() - beginTime));
        ThreadUtil.sleep(15000);
        return str;
    }
}
